package com.example.administrator.readbook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.readbook.R;
import com.example.administrator.readbook.greendao.GreenDaoBookBean;
import com.example.administrator.readbook.widget.slidingbutton.IonSlidingViewClickListener;
import com.example.administrator.readbook.widget.slidingbutton.SlidingButtonView;
import com.example.administrator.readbook.widget.slidingbutton.Utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Administrator on 2018/7/6.
 */

public class AdapterMainListview extends BaseAdapter implements SlidingButtonView.IonSlidingButtonListener {
    private Context context;
    private List<GreenDaoBookBean> bookBeanList;

    public AdapterMainListview(Context context, List<GreenDaoBookBean> bookBeanList) {
        this.context = context;
        this.bookBeanList = bookBeanList;
    }

    @Override
    public int getCount() {
        return bookBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return bookBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final GreenDaoBookBean bookBean = bookBeanList.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_main, null);
            viewHolder = new ViewHolder();
            viewHolder.relativeLayout = convertView.findViewById(R.id.main_layout);
            viewHolder.relativeLayout.getLayoutParams().width = Utils.getScreenWidth(context);
            viewHolder.bookNameTv = convertView.findViewById(R.id.adapter_main_bookname);
            viewHolder.bookLastChapterTv = convertView.findViewById(R.id.adapter_main_bookLastChapter);
            viewHolder.bookImageIv = convertView.findViewById(R.id.adapter_main_bookimage);
            viewHolder.deleteTv = convertView.findViewById(R.id.tv_delete);
            ((SlidingButtonView) convertView).setSlidingButtonListener(AdapterMainListview.this);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.bookNameTv.setText(bookBean.getBookName());
        viewHolder.bookLastChapterTv.setText(bookBean.getBookReadChapter());
        try {
            String urlEncode = URLEncoder.encode(bookBean.getBookImg(), "UTF-8");
            String urlDecode = URLDecoder.decode(urlEncode, "UTF-8");
            String urlDecode2 = URLDecoder.decode(urlDecode, "UTF-8");
            String imageUrl = urlDecode2.substring(7, urlDecode2.length());
            Glide.with(context).load(imageUrl).into(viewHolder.bookImageIv);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIDeleteBtnClickListener.onItemClick(view, position);
            }
        });
        viewHolder.deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIDeleteBtnClickListener.onDeleteBtnCilck(view, position);
            }
        });
        return convertView;
    }


    class ViewHolder {
        LinearLayout relativeLayout;
        TextView bookNameTv, bookLastChapterTv, deleteTv;
        ImageView bookImageIv;
    }

    /******************侧滑删除*********************/
    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    private SlidingButtonView mMenu = null;

    public void setIonSlidingViewClickListener(IonSlidingViewClickListener ionSlidingViewClickListener) {
        mIDeleteBtnClickListener = ionSlidingViewClickListener;
    }

    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }

    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }
    /******************侧滑删除*********************/


}
